###################################################
##名称：钢构钢梁sketchup ruby api类封装代码
##作者：钢之炼筋术士
##日期：2023.10.20 未完成
##测试软件：sketchup pro 2023  
##测试版本：23.0.367 64位
##参考文档：ruby.sketchup.com;www.runoob.com/ruby/
##################################################



class GL
    
    def initialize(bClk,eClk,h,b,fh,yh,cpModelName,cpNumName,cpType)

      ########钢梁类型编号说明########
      @cpType = cpType
      ### 1 = H型钢梁 参数说明：h-高；b-宽；fh-腹板厚;yh-翼缘厚；
      ### 2 = □型钢梁 参数说明：h-高；b-宽；fh-壁厚;
      
      #####构造对应参数######
        @bClkPos = bClk                             #起始点坐标
        @eClkPos = eClk                             #结束点坐标

        @h = h.to_f.mm                              #截面高
        @b = b.to_f.mm                              #截面宽
        @fh = fh.to_f.mm                            #腹板厚
        @yh = yh.to_f.mm                            #翼缘厚

        @cpModelName = cpModelName                  #构件型号
        @cpNumName = cpNumName                      #构件编号

        #####换算参数########
        @cpVector = bClk.vector_to(eClk)                                            #线型构件的向量
        @angToNormal = @cpVector.angle_between [0,0,1]                              #与Zaixs的夹角，弧度值
        @crossVector = @cpVector.cross [0,0,-1]                                     #与Zaixs的差积向量
        @rota = Geom::Transformation.rotation([0,0,0], @crossVector, @angToNormal)  #转动参数

        @cpVectorToOxyPlane = Geom::Vector3d.new(@cpVector.x,@cpVector.y,0)
        @angToXaixsAndxyVt = @cpVectorToOxyPlane.angle_between [1,0,0] 
        if @eClkPos.y < @bClkPos.y
          @angToXaixsAndxyVt = 360.degrees - @angToXaixsAndxyVt
        end
                                                              
        @cplength = bClk.distance(eClk)                       #线型构件的长度
        @numOfinch = 25.4                                     #英寸毫米换算参数

        ####群组结构######
        @model = Sketchup.active_model
        @group = @model.active_entities.add_group      #最外层群组

      
    end

    def drawGL
    
      drawHGL if @cpType == 1
      drawRectPipeGL if @cpType == 2
    
    end

    def drawHGL

      points = []
      points << [0, 0, 0]
      points << [@yh, 0, 0]
      points << [@yh, (@b-@fh)/2, 0]
      points << [@h-@yh, (@b-@fh)/2, 0]
      points << [@h-@yh, 0, 0]
      points << [@h, 0, 0]
      points << [@h, @b, 0]
      points << [@h-@yh, @b, 0]
      points << [@h-@yh, (@b+@fh)/2, 0]
      points << [@yh, (@b+@fh)/2, 0]
      points << [@yh, @b, 0]
      points << [0, @b, 0]
      face = @group.entities.add_face(points)
      #@group.entities.transform_entities(Geom::Transformation.new([@h/2,@b/2,0]), face)
      @group.entities.transform_entities(@rota, face)
      @group.entities.transform_entities(Geom::Transformation.rotation([0,0,0], @cpVector, @angToXaixsAndxyVt ), face)
      @group.entities.transform_entities(Geom::Transformation.new(@bClkPos), face)
      face.pushpull(-@cplength,true)

    end

    def drawRectPipeGL
    
      points = []
      points << [0, 0, 0]
      points << [@h, 0, 0]
      points << [@h, @b, 0]
      points << [0, @b, 0]
      face1 = @group.entities.add_face(points)
      points = []
      points << [@fh, @fh, 0]
      points << [@h-@fh, @fh, 0]
      points << [@h-@fh, @b-@fh, 0]
      points << [@fh, @b-@fh, 0]
      
      face2 = @group.entities.add_face(points)
      @group.entities.erase_entities(face2)
      
      @group.entities.transform_entities(@rota, face1)
      @group.entities.transform_entities(Geom::Transformation.rotation([0,0,0], @cpVector, @angToXaixsAndxyVt ), face1)
      @group.entities.transform_entities(Geom::Transformation.new(@bClkPos), face1)
      face1.pushpull(-@cplength,true)

    
    
    end


    def self.create_GL(params)
    
      my_tool = MyTool.new(params)
      Sketchup.active_model.select_tool(my_tool)
    
    end

    def self.create_Dialog

      dialog = UI::HtmlDialog.new(
          {
            :dialog_title => "创建钢梁类",
            :preferences_key => "com.sample.plugin",
            :scrollable => true,
            :resizable => true,
            :width => 600,
            :height => 800,
            :left => 100,
            :top => 100,
            :min_width => 50,
            :min_height => 50,
            :max_width =>1000,
            :max_height => 1000,
            :style => UI::HtmlDialog::STYLE_DIALOG
          })
          dialog.set_file File.join(File.dirname(__FILE__), 'htmldialog/GL.html')
          dialog.show
          dialog.add_action_callback("confirm") { |action, params|
              # params即js传过来的值
              create_GL(params)
          }

          dialog.add_action_callback("cancel"){
              dialog.close
          }


  end


end


#gl = GL.new([0,0,0],[100,100,100],750,400,16,38,'GKL1','GKL1-A-1',2)
#gl.drawGL



class MyTool
  def initialize(pos1,pos2)
    @pos1 = pos1
    @pos2 = pos2
    @isFirst = true
  end

    def onLButtonDown(flags, x, y, view)

        ip = Sketchup::InputPoint.new
        ip.pick(view, x, y)
        pt = ip.position
        p pt
        if @isFirst
          @pos1 = pt
          @isFirst = false
        else
          @pos2 = pt
          gl = GL.new(@pos1,@pos2,750,400,16,38,'GKL1','GKL1-A-1',1)
          gl.drawGL
          @isFirst = true
        end
        
    end
    def activate
      # 屏幕坐标
      @scr_pt = nil 
      # 模型坐标
      @pick_pt = nil 
      # 光标位置
      @mouse_ip = Sketchup::InputPoint.new
    end
    
    # 监听工具被停用
    def deactivate(view)
      # 将视图标记为需要重绘
      view.invalidate
    end

    # 监听鼠标移动
  def onMouseMove(flags, x, y, view)
    @scr_pt = Geom::Point3d.new(x, y, 0)
    @mouse_ip.pick(view, x, y)
    @pick_pt = @mouse_ip.position
    # 将视图标记为需要重绘
    view.invalidate
  end
    # 监听刷新视图以允许工具进行绘图
  def draw(view)
    @mouse_ip.draw(view) if @mouse_ip.valid?
    return unless @scr_pt && @pick_pt
    draw_pos = @scr_pt.offset(X_AXIS, 20)
    view.draw_text(draw_pos, @pick_pt.to_s, color: 'orange', :size => 24)
  end



  end
  
  my_tool = MyTool.new([0,0,0],[100,100,100])
  Sketchup.active_model.select_tool(my_tool)